﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Data;

using System.Security.Cryptography;


namespace ADUSWebApp.DBConnect
{
    public class DB
    {

        ///<Summary>
        ///连接数据库
        ///</Summary>
        ///<returns>返回SqlConnection对象</returns>
        ///

        public SqlConnection GetCon()
        {
            return new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
        }

        ///<Summary>
        ///执行SQL 语句
        ///</Summary>
        ///<param name = "cmdstr">执行语句</param>
        ///<returns>返回值为int型，成功返回1，失败返回0</returns>
        ///

        public int SqlEx(string cmdstr)
        {
            SqlConnection con = GetCon();   // 打开数据库
            con.Open();     //打开连接
            SqlCommand cmd = new SqlCommand(cmdstr, con);
            try
            {
                cmd.ExecuteNonQuery();  //执行SQL语句并返回受影响的行数
                return 1;   //成功返回1
            }
            catch (Exception e)
            {
                return 0;   //失败返回0
            }
            finally
            {
                con.Dispose();  //释放连接对象资源
            }
        }

        ///<Summary>
        ///执行SQL 查询语句
        ///</Summary>
        ///<param name = "cmdstr">查询语句</param>
        ///<returns>返回DataTable 数据表</returns>
        ///

        public DataTable reDt(string cmdstr)
        {
            SqlConnection con = GetCon();
            SqlDataAdapter da = new SqlDataAdapter(cmdstr, con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return (ds.Tables[0]);
        }

        ///<Summary>
        ///执行SQL 查询语句
        ///</Summary>
        ///<param name = "str">查询语句</param>
        ///<returns>返回SqlDataReader对象dr</returns>
        ///将执行词句的结果存储在一个SqlDataReader对象中,最后将这个对象返回到调用处

        public SqlDataReader reDr(string str)
        {
            SqlConnection conn = GetCon();
            conn.Open();
            SqlCommand com = new SqlCommand(str, conn);
            SqlDataReader dr = com.ExecuteReader(CommandBehavior.CloseConnection);
            return dr;  //返回的对象的数据是只读的
        }

        ///<Summary>
        ///MD5加密
        ///</Summary>
        ///<param name = "strPwd"被加密的字符串</param>
        ///<returns>返回加密后的字符串</returns>
        ///将加密后形成的字符串返回到调用处
        ///
        public string GetMD5(string strPwd)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] data = System.Text.Encoding.Default.GetBytes(strPwd);//将字符编码为一个字节序列
            byte[] md5data = md5.ComputeHash(data); //计算data字节数组的哈希值
            md5.Clear();
            string str = "";
            for (int i = 0; i < md5data.Length - 1; i++)
            {
                //返回一个新字符串，该字符串通过在此实例中的字符左侧填充指定的 Unicode 字符来达到指定的总长度，从而使这些字符右对齐。
                str += md5data[i].ToString("x").PadLeft(2, '0');
            }
            return str;
        }

    }
}